// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin_JXGM ql1746365080@gmail.com
 * @Date: 2024-10-16 09:35:57
 * @LastEditors: qiulin 1746365080@qq.com
 * @LastEditTime: 2025-01-02 17:18:38
 * @FilePath: \SGFEM\Import\BDFImport\BulkData\Property\PBUSHParser.cpp
 */
#include "PBUSHParser.h"

#include <algorithm>

#include <boost/algorithm/string.hpp>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Property/IPBUSHService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (PBUSH);
/*
// file://./../../Doc/MarkDown/BulkData/PBUSH/PBUSH.md>
*/

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::Import::BDF;
using namespace SG::DataStructure;
namespace
{

    void parsePBUSH_K (const std::vector<std::string>& dividedEntryInfo, size_t rowIndex, FEM::PBUSHData& tempData)
    {
        const size_t basicIndex = rowIndex * 10 + 3;
        for (size_t i = 0; i < 6; i++)
        {
            std::string curStr  = dividedEntryInfo[basicIndex + i];
            bool const  isEmpty = is_all_space (curStr);
            if (!isEmpty)
            {
                tempData.m_K[i] = convertToReal (curStr);
            }
        }
    }

    void parsePBUSH_B (const std::vector<std::string>& dividedEntryInfo, size_t rowIndex, FEM::PBUSHData& tempData)
    {
        const size_t basicIndex = rowIndex * 10 + 3;
        for (unsigned char i = 0; i < 6; i++)
        {
            std::string curStr  = dividedEntryInfo[basicIndex + i];
            bool const  isEmpty = is_all_space (curStr);
            if (!isEmpty)
            {
                tempData.m_B[i] = convertToReal (curStr);
            }
        }
    }

    void parsePBUSH_RCV (const std::vector<std::string>& dividedEntryInfo, size_t rowIndex, FEM::PBUSHData& tempData)
    {
        const size_t basicIndex = rowIndex * 10 + 3;
        for (unsigned char i = 0; i < 4; i++)
        {
            std::string curStr  = dividedEntryInfo[basicIndex + i];
            bool const  isEmpty = is_all_space (curStr);
            if (!isEmpty)
            {
                switch (i)
                {
                case 0:  // SA
                    tempData.m_SA = convertToReal (curStr);
                    break;
                case 1:  // ST
                    tempData.m_ST = convertToReal (curStr);
                    break;
                case 2:  // EA
                    tempData.m_EA = convertToReal (curStr);
                    break;
                case 3:  // ET
                    tempData.m_ET = convertToReal (curStr);
                    break;
                default:
                    break;
                }
            }
        }
    }

    void parsePBUSH_GE (const std::vector<std::string>& dividedEntryInfo, size_t rowIndex, FEM::PBUSHData& tempData)
    {
        const size_t basicIndex        = rowIndex * 10 + 3;
        bool         filed2to6AllEmpty = true;  // 默认GE2-GE6都为空，此时填充GE1否则0

        for (size_t i = 0; i < 6; i++)
        {
            std::string curStr  = dividedEntryInfo[basicIndex + i];
            bool const  isEmpty = is_all_space (curStr);
            if (!isEmpty)
            {
                tempData.m_GE[i]  = convertToReal (curStr);
                filed2to6AllEmpty = false;  // 存在非空GE
            }
            else
            {
                tempData.m_GE[i] = 0.0;
            }
        }
        // GE2-GE6都为空，此时填充GE1
        if (filed2to6AllEmpty)
        {
            for (size_t i = 1; i < 6; i++)
            {
                tempData.m_GE[i] = tempData.m_GE[0];
            }
        }
    }

    void parsePBUSH_M (const std::vector<std::string>& dividedEntryInfo, size_t rowIndex, FEM::PBUSHData& tempData)
    {
        const size_t basicIndex = rowIndex * 10 + 3;
        std::string  curStr     = dividedEntryInfo[basicIndex];
        bool const   isEmpty    = is_all_space (curStr);
        if (!isEmpty)
        {
            tempData.m_M = convertToReal (curStr);
        }
    }

    void parsePBUSH_T (const std::vector<std::string>& dividedEntryInfo, size_t rowIndex, FEM::PBUSHData& tempData)
    {
        const size_t basicIndex = rowIndex * 10 + 3;
        for (unsigned char i = 0; i < 4; i++)
        {
            std::string curStr  = dividedEntryInfo[basicIndex + i];
            bool const  isEmpty = is_all_space (curStr);
            if (!isEmpty)
            {
                switch (i)
                {
                case 0:  // alpha
                    tempData.m_alpha = convertToReal (curStr);
                    break;
                case 1:  // tref
                    tempData.m_tref = convertToReal (curStr);
                    break;
                case 2:  // coinl
                    tempData.m_coinl = convertToReal (curStr);
                    break;
                default:
                    break;
                }
            }
        }
    }
}  // namespace

SG::DataStructure::Common::Status BDF::BULKDATA::ParsePBUSH (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                             std::vector<std::string>&                               dividedEntryInfo)
{
    SG::DataStructure::FEM::PBUSHData tempData;

    // TODO (bug 001): 正确处理 m_domainId
    tempData.m_domainId = 1;

    // 解析固定数据字段
    tempData.m_id       = convertToI64 (dividedEntryInfo[1]);  // 获取ID
    size_t const rowNum = dividedEntryInfo.size () / 10;       // 根据每行的长度确定有多少行
    for (size_t i = 0; i < rowNum; i++)
    {
        const size_t curIndex = i * 10 + 2;                    // 每行从第3个元素开始是标志字段
        std::string& Flag     = dividedEntryInfo[curIndex];
        boost::trim (Flag);                                    // 去除标志字段的空格
        if ("K" == Flag)
        {
            parsePBUSH_K (dividedEntryInfo, i, tempData);      // 解析刚度K
        }
        else if ("B" == Flag)
        {
            parsePBUSH_B (dividedEntryInfo, i, tempData);  // 解析阻尼B
        }
        else if ("GE" == Flag)
        {
            parsePBUSH_GE (dividedEntryInfo, i, tempData);  // 解析结构阻尼GE
        }
        else if ("RCV" == Flag)
        {
            parsePBUSH_RCV (dividedEntryInfo, i, tempData);  // 解析应力或应变系数RCV
        }
        else if ("M" == Flag)
        {
            parsePBUSH_M (dividedEntryInfo, i, tempData);  // 解析质量M
        }
        else if ("T" == Flag)
        {
            parsePBUSH_T (dividedEntryInfo, i, tempData);  // 解析热属性T
        }
    }

    // 将解析后的数据添加到PBUSHService数据库
    return dbServiceFactory->get<IPBUSHService> ()->Add (tempData);
}